<template>
    <div>
        <el-dialog :title="isedit ? '修改菜单' : '添加菜单'" :visible.sync="isshow" center :before-close="esc">
            <el-form :model="form" :rules="rules" ref="formName">
                <el-form-item label="角色名称" :label-width="formLabelWidth" prop="rolename">
                    <el-input v-model="form.rolename" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="角色权限" :label-width="formLabelWidth">
                    <el-tree :data="getmenus" ref="tree" default-expand-all show-checkbox node-key="id"
                        :props="defaultProps">
                    </el-tree>
                </el-form-item>

                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="form.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                        :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="esc">取 消</el-button>
                <el-button type="primary" @click="sure">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { roleadd, roleinfo, roleedit } from '../../request/app'
import { mapGetters, mapActions } from 'vuex'
export default {
    props: ['isshow', 'isedit'],
    data() {
        return {
            dialogTableVisible: false,
            dialogFormVisible: false,
            form: {
                rolename: '',
                menus: '',
                status: ''
            },
            formLabelWidth: '120px',
            rules: {
                rolename: [
                    { required: true, message: '请输入菜单名称', trigger: 'blur' },
                ],
            },
            defaultProps: {
                children: 'children',
                label: 'title'
            }
        };
    },
    mounted() {
        // console.log(this.getmenus);
    },
    computed: {
        ...mapGetters({
            getmenus: 'setshop/getstatemenulist'
        })
    },
    methods: {
        ...mapActions({
            geishoplist: 'setshop/getstatemenulist'
            ,
            ACgetlistpeo: 'setpeople/ACgetlistpeo'
        }),

        // 封装一个供父组件修改的方法
        lookup(id) {
            // 获取一条数据来做回显
            roleinfo({
                id
            })
                .then(res => {
                    //    console.log(res);
                    this.form = res.list
                    this.form.id = id
                    this.$refs.tree.setCheckedKeys(this.form.menus.split(','));
                })
        },
        resize() {
            this.form = {
                rolename: '',
                menus: '',
                status: ''
            }
            this.$refs.formName.resetFields();
             this.$refs.tree.setCheckedKeys([]);
        },
        esc() {
            // console.log(e);
            this.$emit('isshow', false)
            this.resize()
        },
        sure() {
            // console.log(this.form);
            // console.log(this.$refs.tree.getCheckedKeys());
            this.form.menus=this.$refs.tree.getCheckedKeys().join(',')
            // console.log(this.form.menus);
            this.$refs.formName.validate((e) => {

                if (e) {
                    if (this.isedit) {
                        // console.log(11111111);
                        // console.log(this.from);
                        roleedit(this.form)
                            .then(res => {
                                // console.log(res);
                                this.ACgetlistpeo()
                                this.esc()
                                this.resize()
                            })
                    } else {
                        // console.log(222222222);
                        roleadd(this.form)
                            .then(res => {
                                // console.log(res);
                                this.ACgetlistpeo()
                                this.esc()
                                this.resize()
                            })
                    }
                }
            })



        },
    },
    components: {
    },
}
</script>

<style scoped>
</style>
